package cos

import (
	"net/url"
	"github.com/tencentyun/cos-go-sdk-v5"
	"net/http"
	"github.com/tencentyun/cos-go-sdk-v5/debug"
	"xlq-demo/library/config"
	"io"
	"context"
)

type CosConfig struct {
	CosUrl string
	SecretId string
	SecretKey string
}
var cosConfig *CosConfig

func Initial() {
	cosConfig = &CosConfig{
		CosUrl: config.Config.Cos.Url,
		SecretId: config.Config.Cos.SecretId,
		SecretKey: config.Config.Cos.SecretKey,
	}
}

func PutContent(filename string, content io.Reader) error {
	u, _ := url.Parse(cosConfig.CosUrl)
	b := &cos.BaseURL{BucketURL: u}
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  cosConfig.SecretId,
			SecretKey: cosConfig.SecretKey,
			Transport: &debug.DebugRequestTransport{
				RequestHeader:  true,
				RequestBody:    false,
				ResponseHeader: true,
				ResponseBody:   true,
			},
		},
	})
	_,err := c.Object.Put(context.Background(),filename, content,nil)
	return err
}
